/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.action.admin;

import java.util.ArrayList;

import org.apache.struts2.interceptor.validation.SkipValidation;

import kiff.action.AbstractCRUDAction;
import kiff.entity.Forum;
import kiff.entity.ForumAcl;
import kiff.entity.ForumAclEntry;
import kiff.entity.ForumPermission;
import kiff.entity.Role;
import kiff.interceptor.InputErrorsAware;
import kiff.security.AclEntry;
import kiff.service.CoreService;
import kiff.service.ForumAclService;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * Admin Forum Access Control List Action.  Controls the administration of forum permissions.
 * @author Adam
 * @version $Id$
 * 
 * Created on Nov 30, 2008 at 11:13:52 PM 
 */
public class ForumAclEntryAction extends AbstractCRUDAction<ForumAclEntry> implements
		Preparable, ModelDriven<ForumAclEntry>, InputErrorsAware {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = -1284288970872975795L;

	/**
	 * Forum Access Control List.
	 */
	private ForumAclEntry aclEntry;

	/**
	 * Forum ACL ID. Since OGNL doesn't support generics I am forced not to be able to use the permissions.id field.
	 */
	private Integer aclId;

	/**
	 * Entity ID. Since OGNL doesn't support generics I am forced not to be able to use the acl id field.
	 */
	private Integer eid;

	/**
	 * Forum Access Control List Service.
	 */
	private ForumAclService forumAclService;

	/**
	 * Role ID. Since OGNL doesn't support generics I am forced not to be able to use the role id field.
	 */
	private Integer rid;

	/**
	 * Principal ID. Since OGNL doesn't support generics I am forced not to be able to use the forum id field.
	 */
	private Integer pid;

	/**
	 * Returns aclId.
	 * @return the aclId.
	 */
	public Integer getAclId() {
		return aclId;
	}

	/**
	 * Returns eid.
	 * @return the eid.
	 */
	public Integer getEid() {
		return eid;
	}

	/**
	 * {@inheritDoc}
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	@Override
	public ForumAclEntry getModel() {
		return aclEntry;
	}

	/**
	 * Returns rid.
	 * @return the rid.
	 */
	public Integer getRid() {
		return rid;
	}

	/**
	 * Prepares the page to display a list of entities.
	 * @return LIST.
	 */
	@SkipValidation @Override @kiff.util.transaction.annotation.Transactional
	public String list() {
		ForumAcl acl = (ForumAcl) forumAclService.findByOwner(aclEntry.getAcl().getOwner());
		this.list = new ArrayList<ForumAclEntry>();
		for (AclEntry entry : acl.getEntries()) {
			this.list.add((ForumAclEntry) entry);
		}
		setActionMethod(UPDATE);
		return LIST;
	}

	/**
	 * {@inheritDoc}
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		if (aclEntry == null) {
			Forum forum = new Forum();
			forum.setId(getPid());
			ForumAcl acl = new ForumAcl(forum);
			acl.setId(getAclId());
			Role role = new Role();
			role.setId(getRid());
			aclEntry = new ForumAclEntry(acl, role, new ForumPermission());
			aclEntry.setId(getEid());
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.interceptor.InputErrorsAware#prepareInputErrors()
	 */
	@Override @kiff.util.transaction.annotation.Transactional
	public void prepareInputErrors() {
		ForumAcl acl = (ForumAcl) forumAclService.findByOwner(aclEntry.getAcl().getOwner());
		this.list = new ArrayList<ForumAclEntry>();
		for (AclEntry entry : acl.getEntries()) {
			this.list.add((ForumAclEntry) entry);
		}
	}

	/**
	 * Sets aclId.
	 * @param aclId the aclId to set.
	 */
	public void setAclId(final Integer aclId) {
		this.aclId = aclId;
	}

	/**
	 * Sets eid.
	 * @param eid the eid to set.
	 */
	public void setEid(final Integer eid) {
		this.eid = eid;
	}

	/**
	 * Sets forumAclService.
	 * @param forumAclService the forumAclService to set.
	 */
	@Inject
	public void setForumAclService(final ForumAclService forumAclService) {
		this.forumAclService = forumAclService;
	}

	/**
	 * Sets rid.
	 * @param rid the rid to set.
	 */
	public void setRid(final Integer rid) {
		this.rid = rid;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.action.AbstractCRUDAction#setService(kiff.service.CoreService)
	 */
	@Inject
	public void setService(final CoreService<ForumAclEntry> service) {
		this.service = service;
		
	}

	/**
	 * Returns pid.
	 * @return the pid.
	 */
	public Integer getPid() {
		return pid;
	}

	/**
	 * Sets pid.
	 * @param pid the pid to set.
	 */
	public void setPid(final Integer pid) {
		this.pid = pid;
	}
}
